﻿using BCL.BaseLib;
using BCL.WPFComponent.Base;
using Devices;
using DevicesView.Logic.Test;
using DevicesView.Model.ViewData;
using System.Collections.Generic;
using System.Linq;
namespace DevicesView.UCControl
{
    /// <summary>
    /// UCTestItems.xaml 的交互逻辑
    /// </summary>
    public partial class UCTestItems : BaseFunctionControl
    {
        Result result;
        public UCTestItems()
        {
            InitializeComponent();
        }

        public UCTestItems(Result result)
        {
            result = new ResultLogic().GetResultById(46);
            InitializeComponent();
            if (result.ResultDatas != null && result.ResultDatas.Count > 0)
                foreach (var item in result.ResultDatas)
                    stackPanelMain.Children.Add(new UCTestItem(item));
            this.textRe.Text = result.Remarks?.First();
        }

        public override void LanguageBinding()
        {

        }

        public UCTestItems(FunctionEntity fe)
        {
            TestResult testResult = new ResultLogic().GetResultById(46);
            InitializeComponent();
            Result = testResult;
        }
        public Result Result
        {
            get
            {
                if (result == null)
                    return null;
                if (result.ResultDatas == null)
                    result.ResultDatas = new List<ResultItem>();
                result.ResultDatas.Clear();

                foreach (var item in stackPanelMain.Children)
                {
                    if (!(item is UCTestItem ucitem))
                        continue;
                    result.ResultDatas.Add(ucitem.ResultItem);
                }

                if (result.Remarks == null)
                    result.Remarks = new List<string>();
                result.Remarks.Clear();
                result.Remarks.Add(textRe.Text);

                return result;
            }
            set
            {
                result = value;
                stackPanelMain.Children.Clear();
                textRe.Text = "";
                if (result == null)
                    return;
                if (result?.ResultDatas != null && result.ResultDatas.Count > 0)
                    foreach (var item in result.ResultDatas)
                        stackPanelMain.Children.Add(new UCTestItem(item));
                
                this.textRe.Text = result.Remarks?.FirstOrDefault();
            }
        }

    }
}
